package model

import (
	"gorm.io/gorm"
	"time"
)

// Model 通用对象模型数据
type Model struct {
	Id         string         `json:"id" gorm:"primaryKey"`
	CreateTime string         `json:"createTime" gorm:"comment:'创建时间'"`
	CreateId   string         `json:"createId" gorm:"comment:'创建者ID'"`
	CreateUser *User          `gorm:"foreignKey:CreateId;"`
	UpdateTime string         `json:"updatedTime" gorm:"default:null;comment:'更新时间'"`
	UpdateId   string         `json:"updateId" gorm:"comment:'修改用户ID'"`
	UpdateUser *User          `gorm:"foreignKey:UpdateId;"`
	DeleteTime gorm.DeletedAt `json:"deleteTime" gorm:"index;comment:'删除时间'"`
	DeleteId   string         `json:"deleteId" gorm:"comment:'删除者ID'"`
	DeleteUser *User          `gorm:"foreignKey:DeleteId;"`
	Memo       string         `json:"memo" gorm:"default:null;comment:'备注'"`
}

const dataTimeFormat = "2006-01-02 15:04:05"

func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
	now := time.Now().Format(dataTimeFormat)
	m.CreateTime = now
	return nil
}
func (m *Model) BeforeSave(tx *gorm.DB) (err error) {
	now := time.Now().Format(dataTimeFormat)
	m.UpdateTime = now
	return nil
}
func (m *Model) BeforeUpdate(tx *gorm.DB) (err error) {
	now := time.Now().Format(dataTimeFormat)
	m.UpdateTime = now
	return nil
}

//func (m *Model) BeforeDelete(tx *gorm.DB) (err error) {
//	now := time.Now().Format(dataTimeFormat)
//	m.DeleteTime = now
//	return nil
//}
